package com.jenings.shop.controller;

import com.alibaba.fastjson.JSON;
import com.jenings.shop.entity.Notice;
import com.jenings.shop.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(tags = {"公告gg"})
@RestController
@RequestMapping("/noticeApi")
public class NoticeController {
    @Autowired
    NoticeService noticeService;
    //查询全部公告
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "查询全部公告")
    @RequestMapping(value = "/selectAll", method = RequestMethod.POST)
    public List<Notice> selectAll(Model model){
        //获取redis中key="userJSON"的value
        List<Notice> noticeList ;
        long startTime;
        long endTime;
        startTime = System.currentTimeMillis();
        String userJSON = (String)redisTemplate.opsForValue().get("userJSON");

        if(userJSON!=null){
            endTime = System.currentTimeMillis();
            System.out.println("取缓存成功，耗时" + (endTime-startTime) + "ms");
           //System.out.println(userJSON);
        }
         noticeList = noticeService.selectAll();
        userJSON = JSON.toJSONString(noticeList);
        endTime = System.currentTimeMillis();
        //将查询结果存入redis中作缓存
        redisTemplate.opsForValue().set("userJSON",userJSON,100, TimeUnit.SECONDS);
        model.addAttribute("gg",noticeList);
        System.out.println(userJSON);
        System.out.println("取缓存失败，耗时" + (endTime-startTime) + "ms");
        return noticeList;
    }
    //查询部分公告
    @ApiOperation(value = "查询部分公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ggnr", value = "公告内容", required = true, dataTypeClass = String.class)
    })
    @RequestMapping(value = "/selectSome", method = RequestMethod.POST)
    public List<Notice> selectSome(@RequestParam(value = "ggnr") String ggnr){
        List<Notice> noticeList = noticeService.selectSome(ggnr);
        return noticeList;
    }
    //查询单独公告
    @ApiOperation(value = "查询单独公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ggnr", value = "公告内容", required = true, dataTypeClass = String.class)
    })
    @RequestMapping(value = "/selectByte", method = RequestMethod.POST)
    public Notice selectByte(@RequestParam(value = "ggid") Integer ggid){
        Notice notice = noticeService.selectByte(ggid);
        return notice;
    }
    //删除公告
    @ApiOperation(value = "删除公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ggid", value = "公告id", required = true, dataTypeClass = String.class)
    })
    @RequestMapping(value = "/delByte", method = RequestMethod.POST)
    public boolean delByte(@RequestParam(value = "ggid") Integer ggid){
        return noticeService.delByte(ggid);
    }
    //增加公告
    @ApiOperation(value = "添加公告")
    @RequestMapping(value = "/addByte", method = RequestMethod.POST, produces = "application/json")
    public boolean addByte(@RequestBody Notice notice){
        return noticeService.addByte(notice);
    }
}
